package com.guli.mall.serviceBase.sentinel;
/*
 * @Author 罗俊
 * @date 2020/12/6 - 9:48 下午
 *
 */

import com.alibaba.csp.sentinel.adapter.servlet.CommonFilter;
import com.alibaba.csp.sentinel.adapter.servlet.callback.UrlBlockHandler;
import com.alibaba.csp.sentinel.adapter.spring.webflux.callback.BlockRequestHandler;
import com.alibaba.csp.sentinel.slots.block.BlockException;
import com.alibaba.fastjson.JSON;
import com.guli.common.pojo.vo.StatusCodeEnum;
import com.guli.common.utils.R;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.web.server.ServerWebExchange;

import javax.servlet.Filter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class MySentinelBlockRequestHandler implements UrlBlockHandler {


    @Override
    public void blocked(HttpServletRequest httpServletRequest, HttpServletResponse response, BlockException e) throws IOException {

        R error = R.error(StatusCodeEnum.SENTINEL_TO_MANY_REQUESTS);

        response.setContentType("application/json");

        response.setCharacterEncoding("utf-8");

        response.getWriter().write(JSON.toJSONString(error));

        response.getWriter().flush();
    }


}
